import { onUnmounted, ref } from "vue";

export default function useCountDown(initTime=10,cb: Function){
    // 计时 时间
    let time = ref<number>(initTime);
    let IntervalTimeId = ref<number>(0);
    // let endCallback = ref<Function>(() => {});  
    // endCallback.value = cb;
    const start = ()=>{
        IntervalTimeId.value = setInterval(
            ()=>{
                // 计时结束
                if(time.value === 0){
                    clearInterval(IntervalTimeId.value);
                    // endCallback.value();
                    cb?.();
                    return;
                }
                time.value = time.value-1; 
            }
        ,1000)
    }
    //组件卸载时清理
    onUnmounted(()=>{
        clearInterval(IntervalTimeId.value);
    })
    return {time,start}
}